﻿using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Repositorys
{
    public class OssUserPwdRepository : SqlServerRespository<OssUserPwdEntity>,
        IOssUserPwdRepository
    {
        public OssUserPwdRepository()
        {
            Context = new OssContext();
        }


        public OssUserPwdEntity GetPwdEntity(string username)
        {
            return
                GetList(
                    "SELECT [UserId] as UserId,[username] as UserName,[userpwd] as Userpwd from [oss_user_pwd] where username=@username",
                    new {username}).FirstOrDefault();
        }

        public void SavePwd(OssUserPwdEntity entity, UnitOfWork unit = null)
        {
            if (GetPwdEntity(entity.Userid) != null)
                Modify(entity, unit);
            else
                Add(entity, unit);
        }

        public OssUserPwdEntity GetPwdEntity(long userid)
        {
            return
                GetList(
                    "SELECT [UserId] as UserId,[username] as UserName,[userpwd] as Userpwd from [oss_user_pwd] where userid=@userid",
                    new {userid}).FirstOrDefault();
        }

        public void DeleteUser(long userid, UnitOfWork unitOfWork = null)
        {
            var entity = GetPwdEntity(userid);
            Remove(entity, unitOfWork);
        }
    }
}